package com.ys.controller.web.base;

import com.ys.config.MenuId;
import com.ys.entity.base.TbUserPaySlip;
import com.ys.entity.base.TbUserPersonalTaxCount;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工个税统计表
 */
@Controller
@RequestMapping("/web/base/tbUserPersonalTaxCount")
public class WebTbUserPersonalTaxCountController {
	private static final Logger logger = LoggerFactory.getLogger(WebTbUserPersonalTaxCountController.class);

	@Autowired
	private CommonApiService<TbUserPersonalTaxCount> commonApiService;


    public Map<String, Object> getEntity(Long id){
        Map<String,Object> entity = new TbUserPersonalTaxCount()
        .addLeftJoin("TS_UserInfo U ON U.UserId = A.UserId")//用户信息表
                .addField("(U.UserId + '/' + U.UserName) AS _ys_nameLinked")//工号/姓名

                .addLeftJoin("TS_OrganizationInfo OI ON OI.Orgid = U.DeptId")
                .addField("OI.OrgName AS deptName")//部门

                .addLeftJoin("THR_UserArchives UAS ON UAS.UserId = A.UserId")//用户档案表
                .addField("UAS.UserState AS userState")//员工状态

        .addLeftJoin("TB_UserPaySlip PS ON PS.Id = A.PaySlipId")//员工工资表
                .addField("PS.RealityPaySlip AS realityPaySlip")//实发工资

        .addLeftJoin("TS_OrganizationInfo O ON O.OrgId = A.OrgId")//组织机构表
                .addField("O.OrgName AS orgName")//所属公司

        .addLeftJoin("TB_UserMonthPerformance MP ON MP.Id = PS.MonthPerformanceId")//员工月度绩效信息表
                .addField("MP.Rank AS rank")//岗位职级
                .addField("MP.Type AS type")//岗位工种(0总经理 1副总经理 2责任月薪型员工 3绩效提奖型员工 4研发人员)

                .addLeftJoin("TB_UserPersonalTaxCount AP ON AP.UserId = A.UserId AND AP.CountMonth = A.CountMonthBefore AND AP.OrgId = A.OrgId")//员工个税统计表上个月
                .addField("ISNULL(AP.IncomeAddup,0) AS incomeAddupBefore")//上个月收入累计
                .addField("ISNULL(AP.DeductCostAddup,0) AS deductCostAddupBefore")//上个月扣除费用累计
                .addField("ISNULL(AP.SpecialDeductCostAddup,0) AS specialDeductCostAddupBefore")//上个月专项扣除累计
                .addField("ISNULL(AP.OtherDeductCostAddup,0) AS otherDeductCostAddupBefore")//上个月其他扣除
                .addField("ISNULL(AP.SpecialAddDeductCostAddup,0) AS specialAddDeductCostAddupBefore")//上个月专项附加扣除

            .setId(id)
            .queryFirstMap();

        return entity;
    }


    public Map<String, Object> setExtraParam(Map<String,Object> entity){
        Map<String, Object> extraParam = new HashMap<String, Object>();
        //关联数据
        if(entity.get("_ys_nameLinked")!=null)extraParam.put("_ys_nameLinked", entity.get("_ys_nameLinked"));
        if(entity.get("deptName")!=null)extraParam.put("deptName", entity.get("deptName"));
        if(entity.get("userState")!=null)extraParam.put("userState", entity.get("userState"));
        if(entity.get("orgName")!=null)extraParam.put("orgName", entity.get("orgName"));

        if(entity.get("realityPaySlip")!=null)extraParam.put("realityPaySlip", entity.get("realityPaySlip"));
        if(entity.get("type")!=null)extraParam.put("type", entity.get("type"));
        if(entity.get("rank")!=null)extraParam.put("rank", entity.get("rank"));
        if(entity.get("incomeAddupBefore")!=null)extraParam.put("incomeAddupBefore", entity.get("incomeAddupBefore"));
        if(entity.get("deductCostAddupBefore")!=null)extraParam.put("deductCostAddupBefore", entity.get("deductCostAddupBefore"));
        if(entity.get("specialDeductCostAddupBefore")!=null)extraParam.put("specialDeductCostAddupBefore", entity.get("specialDeductCostAddupBefore"));
        if(entity.get("otherDeductCostAddupBefore")!=null)extraParam.put("otherDeductCostAddupBefore", entity.get("otherDeductCostAddupBefore"));
        if(entity.get("specialAddDeductCostAddupBefore")!=null)extraParam.put("specialAddDeductCostAddupBefore", entity.get("specialAddDeductCostAddupBefore"));
        return extraParam;
    }



    /*
     * 详情
     */
    @GetMapping("/toDetail")
    public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            Map<String,Object> entity = getEntity(id);
            Map<String, Object> extraParam = setExtraParam(entity);
            model.addAttribute("onProcess", 1);
            return commonApiService.toDetail(model,id, new TbUserPersonalTaxCount(), MenuId.TB_UserPersonalTaxCount, extraParam, null);
        }catch (Exception e) {
            logger.error("详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }




}






